<template>
  <div>
    <div>
      <div class="admin-search">
        <el-input
          v-model="text"
          @input="searchs"
          size="mini"
          placeholder="输入关键字搜索"
        />
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="类别ID" prop="gtId"> </el-table-column>
        <el-table-column label="类别名称" prop="gtName"> </el-table-column>
        <el-table-column label="状态" prop="gtState"> </el-table-column>
        <el-table-column align="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >修改状态</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="admin-fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :currentPage="curPage"
          @current-change="page"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getGameType,
  getGameTypeLike,
  updateGameType,
} from "../../network/admin";

export default {
  name: "AdminGameCate",
  data() {
    return {
      tableData: [],
      text: "",
      curPage: 1,
      total: 0,
      state: "",
      loading: true,
    };
  },
  created() {
    this.getMessage();
  },
  methods: {
    getMessage() {
      if (this.$route.query.page != undefined) {
        this.curPage = this.$route.query.page;
      }
      getGameType(this.curPage).then((res) => {
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    handleEdit(row) {
      this.$router.push({
        path: "upatecate",
        query: {
          gtId: row.gtId,
        },
      });
    },
    handleDelete(row) {
      if (row.gtState == 0) {
        this.state = 1;
      } else {
        this.state = 0;
      }
      updateGameType(row.gtId, this.state).then((res) => {
        this.$alert("ID为 : " + row.gtId + "的用户 状态修改成功", "提示", {
          confirmButtonText: "确定",
          callback: () => {
            this.getMessage();
          },
        });
        console.log(res);
      });
      console.log(row);
    },

    searchs() {
      this.curPage = 1;
      this.$router.push({
        path: "admingamecate",
        query: {
          page: 1,
        },
      });
      getGameTypeLike(this.text, this.curPage).then((res) => {
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      });
    },

    page(currentPage) {
      this.curPage = currentPage;
      this.loading = true;
      this.$router.push({
        path: "admingamecate",
        query: {
          page: currentPage,
        },
      });
      this.getMessage();
    },
  },
};
</script>

<style>
.admin-search {
  width: 2rem;
  position: absolute;
  right: 0.5rem;
  z-index: 999;
}

.admin-search input {
  width: 2rem;
}
</style>